//
//  MJComposeView.swift
//  新浪微博
//
//  Created by MaJi on 16/4/16.
//  Copyright © 2016年 MaJi. All rights reserved.
//

import UIKit
import SnapKit

class MJComposeView: UIView {

//    记录控制器
    private var target:UIViewController?
//    将此view添加到MJMainTabViewcontroller上
//    并将控制器传进来,进行控制器的跳转
    func show(target:UIViewController){
        self.target = target
        
//        添加视图
        target.view.addSubview(self)
    }
    override init(var frame: CGRect) {
        frame = UIScreen.mainScreen().bounds
        super.init(frame: frame)
//        backgroundColor = UIColor.redColor()
        setupUI()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        for (index,btn) in self.btnArray.reverse().enumerate(){
            animationWith(index, btn: btn, isup: false)
        }
    }
//    添加到父视图的动画
    override func willMoveToSuperview(newSuperview: UIView?) {
//        遍历按钮数组,取出每一个按钮,改变y值
        for (index,btn) in self.btnArray.enumerate(){
            animationWith(index, btn: btn)
        }
    }
    private func animationWith(index:Int,btn:MJCompostButtton,isup:Bool = true){
        UIView.animateWithDuration(0.8, delay: 0.03 * Double(index), usingSpringWithDamping: 0.7, initialSpringVelocity: 5, options: [], animations: { () -> Void in
            btn.center = CGPoint(x: btn.center.x, y: btn.center.y + (isup ? -350:350))
            }, completion: { (Bool) -> Void in
                if isup == false {
                    self.removeFromSuperview()
                }
        })

    }
    func setupUI(){
        addSubview(backImageView)
        addSubview(sloganView)
 //    添加6个按钮
        addchildBtn()
//        布局子控件
        backImageView.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.snp_edges)
        }
        sloganView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.snp_centerX)
            make.top.equalTo(self.snp_top).offset(100)
        }
    }
//    实现按钮的点击方法,点击的哪个按钮放大,其余缩小
   @objc private func btnDidClick(button:MJCompostButtton){
//        遍历数组,找到点击的那个按钮
        for btn in self.btnArray{
            var scale:CGFloat = 0
            if btn == button{
                scale = 2
            }else{
                scale = 0.2
            }
            UIView.animateWithDuration(0.25, animations: { () -> Void in
                btn.alpha = 0
                btn.transform = CGAffineTransformMakeScale(scale, scale)
                }, completion: { (_) -> Void in

            })
        }
    //                    跳转控制器到发布微博控制器
    //                    获取控制器的名字
    //执行页面跳转
    //获取按钮对应的模型
    guard let vcName = button.compostItem?.target else{
        print("className 为空")
        return
    }
    //需要记忆
    //根据字符串获取对应的类型
    let classType = NSClassFromString(vcName) as! UIViewController.Type
    let vc = classType.init()
    let nav = UINavigationController(rootViewController: vc)
    self.target?.presentViewController(nav, animated: true, completion: {() -> Void in
        
        self.removeFromSuperview()
    })
    }
//    添加6个按钮
    private func addchildBtn(){
        let btnW:CGFloat = 80
        let btnH:CGFloat = 110
        let margin:CGFloat = (screenWidth - btnW*3)/4
        let count = 6
        for i in 0 ..< count {
//            创建按钮
            let btn = MJCompostButtton()
//            给按钮添加点击事件
            btn.addTarget(self, action: "btnDidClick:", forControlEvents: .TouchUpInside)
            btn.compostItem = composeInfos[i]
//            设置按钮的文字的大小
            btn.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
            btn.titleLabel?.font = UIFont.systemFontOfSize(15)
            btn.titleLabel?.textAlignment = .Center
            let row = i/3
            let col = i%3
            let btnX = CGFloat(col)*(btnW+margin)+margin
            let btnY = CGFloat(row)*(btnH+margin) + screenHignt
            btn.frame = CGRect(x: btnX, y: btnY, width: btnW, height: btnH)
            addSubview(btn)
//            添加到数组中进行保存
            btnArray.append(btn)
//            btn.backgroundColor = UIColor.redColor()
        }
    }
//    装按钮的数组
    private var btnArray:[MJCompostButtton] = [MJCompostButtton]()
//    装模型的数组
    private lazy var composeInfos:[MJComposeItem] = {
        //     加载plist文件
        let path = NSBundle.mainBundle().pathForResource("Compose.plist", ofType: nil)!
        let dictArray = NSArray(contentsOfFile: path) as! [[String : String]]
        var tempArray = [MJComposeItem]()
        for dict in dictArray {
            let model = MJComposeItem(dict: dict)
            tempArray.append(model)
        }
        return tempArray
    }()
    

//    子空间的懒加载
    private lazy var backImageView: UIImageView = UIImageView(image: UIImage.snapshotScreen().applyLightEffect())
    private lazy var sloganView: UIImageView = UIImageView(image: UIImage(named: "compose_slogan"))
}
